<?php

namespace App\Http\Controllers\Admin;

use App\Model\Role;
use App\Model\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    //获取列表页
    public function index(Request $request)
    {

        //获取查询提交的请求参数
        $userlist = User::orderBy('user_id','asc')
            ->where(function($query) use($request){
                $username = $request->input('username');
                $email = $request->input('email');
                if (!empty($username)){
                    $query->where('user_name','like','%'.$username.'%');
                }
                if (!empty($email)){
                    $query->where('email','like','%'.$email.'%');
                }

            })->paginate($request->input('num') ? $request->input('num') : 3);
        //用户列表
            return view('admin.user.list')->with(['userlist'=>$userlist,'request'=>$request]);
    }

    /**
     * 返回添加页面
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.user.add');
    }

    /**
     * 执行添加操作.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //接受前台表单提交的数据
        $input = $request->all();
        //进行表单验证


        //添加到数据库

        $username = $input['username'];
        $email = $input['email'];
        $pass = Crypt::encrypt($input['pass']);
        //验真用户是否已存在
        $user = User::where('email',$email)->first();
        if ($user){
            $data = [
                'status'=>1,
                'message'=>'用户已存在'
            ];
            return $data;
        }


        $res = User::create(['user_name'=>$username,'email'=>$email,'user_pass'=>$pass]);

        //根据添加结果给客户端返回json格式数据
        if($res){
            $data = [
                'status'=>0,
                'message'=>'添加成功'
            ];

        }else{
            $data = [
                'status'=>1,
                'message'=>'添加失败'
            ];

        }
        return $data;

    }

    /**
     * 现实一条数据
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

    }

    /**
     * 返回修改页面
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = User::find($id);

        return view('admin.user.edit')->with(['user'=>$user]);
        //
    }

    /**
     * 执行修改操作
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //获取提交的信息

        $user = $request->except('uid');
        //数据验证
        //去除空值
        $user= array_filter($user);

        if(isset($user['user_pass'])){
            unset($user['repass']);
           $user['user_pass'] = Crypt::encrypt($user['user_pass']);
           $res = User::where('user_id',$id)->update($user);
        }else{
            $res = User::where('user_id',$id)->update($user);

        }

        if ($res){
            $data = ['status'=>0,'message'=>'修改成功'];
        }else{
            $data = ['status'=>1,'message'=>'修改失败'];

        }
        return $data;
    }

    /**
     * 执行删除操作
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = User::where('user_id',$id)->delete();

        if($res){
            $data = ['status'=>0,'message'=>'删除成功'];
        }else{
            $data = ['status'=>1,'message'=>'删除失败'];

        }

        return $data;
    }

    //删除所有选中用户
    public function delAll(Request $request)
    {
        $input = $request->input('ids');

        $res = User::destroy($input);
        if($res){
            $data = ['status'=>0,'message'=>'删除成功'];
        }else{
            $data = ['status'=>1,'message'=>'删除失败'];
        }

        return $data;
    }

    //用户角色
    public function Auth($id)
    {
        //获取所有角色
        $role = Role::get();
        //获取当前用户
        $user = User::find($id);
        //获取当前用户拥有的角色
        $self = $user->role;
        $self_id = [];
        //获取当前权限id
        foreach ($self as $v){
            $self_id[] = $v->id;
        }
        return view('admin.user.auth',compact('role','user','self_id'));
    }
    //处理用户更新角色
    public function doAuth(Request $request)
    {
        $input = $request->all();

        //删除用户已有的角色
        \DB::table('user_role')->where('user_id',$input['id'])->delete();

        //添加新的角色
        foreach ($input['role_id'] as $v)
        {
           $res =  \DB::table('user_role')->insert(['user_id'=>$input['id'],'role_id'=>$v]);
        }

        if($res)
        {
            $data = [
                'status' => 0,
                'message'=>'角色更新成功'
            ];
        }else{
            $data = [
                'status' => 1,
                'message'=>'角色更新失败'
            ];
        }

        return $data;
    }

    public function changeStatus(Request $request)
    {
        $input = $request->input('id');

        $user = User::find($input);

        if($user->status == 1){
            $user->status = 0;
        }else{
            $user->status = 1;
        }

        if($user->save()){
            $data = [
                'status'=>0,
                'message'=>'状态切换成功！'
            ];
        }else{
            $data = [
                'status'=>1,
                'message'=>'状态切换失败！'
            ];
        }
        return $data;
    }
}
